<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\jzshop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\jzshop\model\Goods;
use app\jzshop\model\GoodsSku;
use app\jzshop\model\GoodsSkuStandby;
use app\jzshop\model\Supcorp;
use app\jzshop\model\Supplier as SupplierModel;
use think\Db;
use think\facade\View;

/**
 * 供应商控制器
 * @package app\jzshop\admin
 */
class Supgoodssku extends Admin
{
    /**
     * 供应商列表
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 查询
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder('supplierId asc,itemSpuId asc,itemId desc');
        // 数据列表
        $data_list = GoodsSku::where($map)->where('id > 0')->order($order)->paginate();
        // dump(Db::getLastSql());die;
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTips('debug:' . Db::getLastSql(), 'warning')
            ->setSearch(['supplierId' => '供应商ID', 'itemName' => '商品名称', 'erpStyleCode' => '资料编码', 'itemSpuId' => '商品供应链', 'colour' => '商品颜色', 'itemCode' => 'SKU编码', 'supplyPrice' => '供应价格', 'spec' => '尺寸', 'weight' => '重量']) // 设置搜索框
            ->addFilter('jzshop_goods_sku.itemName,jzshop_goods_sku.colour,jzshop_goods_sku.itemCode') // 添加筛选
            ->addFilter('jzshop_goods_sku.supplierId', Supcorp::column('coName', 'supplierId')) // 添加筛选
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['pic', '规格图', 'img_url'],
                ['supplierId', '供应商ID', 'text'],
                ['itemName', '商品名称', 'text'],
                ['erpStyleCode', '资料编码', 'text'],
                ['itemSpuId', '商品供应链', 'text'],
                ['colour', '商品颜色', 'text'],
                ['itemCode', 'SKU编码', 'text'],
                ['supplyPrice', '供应价格', 'text'],
                ['spec', '尺寸', 'text'],
                ['weight', '重量', 'text'],
                ['linePrice', '分销控价', 'text'],
                ['dealPriceNinetyMax', '最高成交', 'text'],
                ['right_button', '操作', 'btn']
            ])
            ->addOrder('supplyPrice')
            ->addTopButtons('enable,disable') // 批量添加顶部按钮           
            ->addRightButtons('enable,disable') // 批量添加顶部按钮       
            ->addRightButton('view', [
                'title' => '预览',
                'icon'  => 'fa fa-fw fa-magic',
                'class' => 'btn btn-success',
                'href'  => url('view', ['goods_id' => '__id__'])
            ], ['area' => ['600px', '90%']])
            ->addRightButton('link', [
                'title' => '聚水潭',
                'icon'  => 'fa fa-fw fa-magic',
                'class' => 'btn btn-success',
                'target' => '_blank',
                'href'  => 'https://gyl.scm121.com/channel/goodsDetailNew/__itemSpuId__?itemCoId=__coId__'
            ])
            // ->addOrder(['supplierId' => 'asc', 'itemSpuId' => 'asc', 'itemId' => 'desc'])
            ->setRowList($data_list) // 设置表格数据

            ->fetch(); // 渲染模板
    }


    /**
     * 启用友情链接
     * @param array $record 行为日志
     * @author 蔡伟明 <314013107@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function enable($record = [])
    {
        $ids =  $this->request->param('ids');
        if (is_string($ids)) {
            $ids = [$ids];
        }

        foreach ($ids as $id) {
            $goodsSku = GoodsSku::where(['id' => $id])->find();
            $check = GoodsSkuStandby::field('itemCode')->where('itemCode', $goodsSku['itemCode'])->find();
            if (!$check) {
                GoodsSkuStandby::saveGoods($goodsSku);
            } else {
                $this->error($goodsSku['itemName'] . '商品已存在');
            }
        }
        $this->success('启用成功');
    }


    public function view($goods_id = null)
    {
        if ($goods_id === null) $this->error('缺少参数');

        $goods_sku = GoodsSku::where(['id' => $goods_id])->find();
        $goods = Goods::where(['itemSpuId' => $goods_sku['itemSpuId']])->find();
        $main_img = $goods_sku['pic'];
        $header_imgs = json_decode($goods['itemMainImage'], true);
        $body_imgs = json_decode($goods['itemDetailImages'], true);

        View::assign('header', array_merge([$main_img], $header_imgs));
        View::assign('body', $body_imgs);
        View::assign('goods_sku', $goods_sku);
        View::assign('goods', $goods);

        return View::fetch();
    }
}
